package com.liaogui.consumer.controller.sys;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liaogui.common.entity.userinfo.UserInfoEntity;
import com.liaogui.common.service.userinfo.UserInfoService;
import com.liaogui.common.utils.R;
import com.liaogui.consumer.service.PasswordService;
import org.apache.dubbo.config.annotation.Reference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/")
public class LoginController {
    @Reference
    private UserInfoService userInfoService;
    @Autowired
    private PasswordService passwordService;
    @Autowired
    public static Logger logger = LoggerFactory.getLogger(LoginController.class);
//    @RequestMapping(value = "register",method = RequestMethod.POST)
//    public R register(HttpServletRequest request){
//        UserInfoEntity userInfo = new UserInfoEntity();
//        String userName = request.getParameter("NAME");
//        String phone = request.getParameter("PHONE");
//        String email = request.getParameter("EMAIL");
//        String password = request.getParameter("PASSWORD");
//        userInfo.setEmail(email);
//        userInfo.setPhone(phone);
//        userInfo.setPassword(password);
//        userInfo.setUserName(userName);
//        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
////        queryWrapper.eq("phone",phone).last("LIMIT 1");
//        queryWrapper.eq("id","11111");
//        try {
//            UserInfoEntity userInfoEntity= userInfoService.selectUserInfo(phone);
//            if (null != userInfoEntity){
//                return R.error("该手机号码已经注册");
//            }
//            userInfoService.save(userInfo);
//            return R.ok();
//        }catch (Exception e){
//            e.printStackTrace();
//            logger.error("注冊失败");
//            return R.error("注册失败");
//        }
//
//    }
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public R login(HttpServletRequest request){
        String phone = request.getParameter("PHONE");
        String password = request.getParameter("PASSWORD");
        UserInfoEntity userInfoEntity = userInfoService.selectUserInfo(phone);
        if (null == userInfoEntity){
            return R.error("该手机号码未注册");
        }
        String encryptPwd = passwordService.encryptPassword(phone, userInfoEntity.getSalt(), password);
        if (encryptPwd.equals(userInfoEntity.getPassword())){
            return R.ok();
        }
        return R.error("登录失败");

    }
}
